// hyb_yl/onesubpages/pages/recipe/index.js
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    repicelist: '',
    tempFilePaths: '',
    isshow: true,
    clinic: 0,
    mew: false,
    mess: {},
    sickness: [{
        title: "上呼吸道感染",
        isshow: false
      },
      {
        title: "咽炎",
        isshow: false
      },
      {
        title: "支气管炎",
        isshow: false
      },
      {
        title: "肺炎",
        isshow: false
      },
      {
        title: "幽门螺旋杆菌感染",
        isshow: false
      },
      {
        title: "牙龈炎",
        isshow: false
      }
    ],
    choosesick: true,
    take: true,
    maskbox: false,
    //过敏
    allergy: false,
    reflect: false,
    arguee: false,
    pic: [],
    related: [],
    intrArr: [{
        ques: "您是否服用过该药品且无相关禁忌症",
        type: "a",
        isshow: true
      },
      {
        ques: "您是否度该药物过敏",
        type: "b",
        isshow: false
      },
      {
        ques: "有无不良反应",
        type: "c",
        isshow: false
      }
    ],
    repice: [],
    ifyyr: false
  },
  chooseimage: function() {
    var that = this;
    var uniacid = app.siteInfo.uniacid;
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function(res) {
        var imgsrc = res.tempFilePaths;
        console.log(imgsrc)
        for (var i = 0; i < imgsrc.length; i++) {
          wx.uploadFile({
            url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
            filePath: imgsrc[i],
            name: 'upfile',
            formData: {},
            success: function(res) {
              console.log(res.data)
              if (that.data.clinic == 0) {
                var pic = that.data.pic
                if (pic.length < 9) {
                  pic.push(res.data)
                  console.log(pic)
                  that.setData({
                    pic: pic
                  });
                } else {
                  wx.showToast({
                    title: '最多可添加九张图片',
                    icon: 'none'
                  })
                }
              } else if (that.data.clinic == 1) {
                var repice = that.data.repice
                if (repice.length < 9) {
                  repice.push(res.data)
                  that.setData({
                    repice: repice
                  })
                } else {
                  wx.showToast({
                    title: '最多可添加九张图片',
                    icon: 'none'
                  })
                }
              }
            }
          })
        }


      },
    })
  },
  // 删除图片
  delImg: function(e) {
    console.log(e)
    var pic = this.data.pic;
    var index = e.currentTarget.dataset.index;
    if (this.data.clinic == 0) {
      pic.splice(index, 1)
      this.setData({
        pic: pic
      })
    } else if (this.data.clinic == 1) {
      var repice = this.data.repice;
      repice.splice(index, 1)
      this.setData({
        repice: repice
      })
    }
  },
  make: function(e) {
    var id = e.currentTarget.dataset.id
    var sickness = this.data.sickness
    sickness[id].isshow = !sickness[id].isshow
    this.setData({
      sickness: sickness
    })
    var related = []
    for (var i = 0; i < this.data.sickness.length; i++) {
      if (this.data.sickness[i].isshow == true) {
        related.push(this.data.sickness[i].title)
      }
    }
    this.setData({
      related: related
    })
  },
  clinic: function(e) {
    this.setData({
      clinic: e.currentTarget.dataset.id
    })
  },
  // 复诊开方
  take_yes: function(e) {
    console.log(e)
    // var item = e.dataset.item;
    var that = this
    var intrArr = that.data.intrArr;
    var index = e.currentTarget.dataset.index
    var type = e.currentTarget.dataset.type
    intrArr[index].isshow = !intrArr[index].isshow;
    if (intrArr[0].isshow == true && intrArr[1].isshow == false && intrArr[2].isshow == false) {
      this.setData({
        arguee: false
      })
    } else {
      this.setData({
        arguee: true
      })
    }
    if (intrArr[index].isshow) {

      if (index == 1 || index == 2) {
        wx.showModal({
          title: '',
          content: '为了您的用药安全，建议您到线下门诊',
          showCancel: false,
          confirmText: "我知道了",
          confirmColor: "#ff0000"
        })
        this.setData({
          arguee: true
        })
      }
    } else {
      if (index == 0) {
        wx.showModal({
          title: '',
          content: '为了您的用药安全，建议您到线下门诊',
          showCancel: false,
          confirmText: "我知道了",
          confirmColor: "#ff0000"
        })
        this.setData({
          arguee: true
        })
      }
    }
    that.setData({
      intrArr: intrArr
    })
    console.log(intrArr)
    // if(this.data.take == true && this.data.allergy==false || this.data.allergy == true && this.data.reflect==false || this.data.reflect==true){
    //   this.setData({
    //     take:true,
    //     arguee:true
    //   })
    // }else{
    //   this.setData({
    //     take: true,
    //     arguee: false
    //   })
    // }

  },
  // 遮罩层
  // know:function(){
  //   this.setData({
  //     maskbox:!this.data.maskbox
  //   })
  // },
  succ: function() {
    var that = this
    var ifyyr = this.data.ifyyr
    var clinic = this.data.clinic;
    if (!ifyyr) {
      wx.showToast({
        title: '请选择用药人',
        icon: 'none'
      })
      return false;
    }
    if (clinic == 0) {
      var intrArr = this.data.intrArr
      var pic = this.data.pic
      console.log(pic)
      var list = {
        intrArr: intrArr,
        pic: pic
      }
      console.log(list)
    } else if (clinic == 1) {
      var list = this.data.repice
    }
    console.log(clinic)
    app.util.request({
      'url': 'entry/wxapp/Goods.cfcreate',
      data: {
        openid: wx.getStorageSync('openid'),
        content: list,
        j_id: that.data.j_id,
        typeSate: parseInt(clinic)
      },
      success: function(res) {
        console.log(res)
        var cf_id = res.data
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];
        prevPage.setData({
          j_id: that.data.j_id,
          cf_id: cf_id
        })
        wx.navigateBack({
          detail: 1
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    if (this.data.allergy = true || this.data.allergy == false || this.data.reflect == false) {
      this.setData({
        arguee: false
      })
    }
    if (this.data.tempFilePaths == "") {
      this.setData({
        isshow: false
      })
    }
    var medicemess = wx.getStorageSync("medicemess")
    var mess = medicemess.slice(0, 1)
    if (medicemess == '') {
      this.setData({
        new: false
      })
    } else {
      this.setData({
        new: true,
        mess: mess
      })
    }
    var medicemess = wx.getStorageSync("medicemess")
    console.log(medicemess)
    var repicelist = medicemess.slice(0, 1)
    this.setData({
      repicelist: repicelist
    })
    app.util.request({
      'url': 'entry/wxapp/Hzbingli.url',
      success: function(res) {
        console.log(res)
        that.setData({
          url: res.data
        })
      },
    })
  },
  pharmacy: function() {
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi',
    })
  },
  selectdrug: function() {
    wx.navigateTo({
      url: '/hyb_yl/onesubpages/pages/selectdrug/selectdrug',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    console.log(currPage.data.names)
    if (currPage.data.names) {
      var that = this
      var j_id = currPage.data.j_id
      app.util.request({
        'url': 'entry/wxapp/User.detailmyjd',
        data: {
          j_id: j_id
        },
        success: function(res) {
          console.log(res)
          that.setData({
            age: res.data.age,
            sex: res.data.sex,
            phone: res.data.tel,
            username: res.data.names,
            new: true,
            ifyyr: true,
            j_id: j_id
          })
        }
      })
    }
    if (this.data.allergy = true || this.data.allergy == false || this.data.reflect == false) {
      this.setData({
        arguee: false
      })
    }
    var medicemess = wx.getStorageSync("medicemess")
    console.log(medicemess)
    var repicelist = medicemess.slice(0, 1)
    this.setData({
      repicelist: repicelist
    })
    console.log(repicelist)
    if (medicemess == "") {
      this.setData({
        new: false
      })
    } else {
      this.setData({
        new: true,
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

})